// 选择生产计划 

<template>
  <view class="search-button-page-200">
    <mes-search
      placeholder="计划编号"
      v-model="postForm.purchase_code"
      @search="resetPaging(), getData()"
    ></mes-search>
    <mes-card-radio ref="mesCardRadio" :form="cardForm"> </mes-card-radio>
    <mes-tip v-if="noData"></mes-tip>
    <mes-button type="2" :form="buttonForm"></mes-button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      noData: false,
      hasMore: true,
      postForm: {
        purchase_code: '',
        page: 1,
        examine_status: '1'
      },
      buttonForm: {
        text: '保存',
        confirmEvent: () => {
          let checked = this.$refs.mesCardRadio.getChecked()
          if (!this.cardForm.checked) {
            return uni.showToast({
              title: '请选择生产计划',
              icon: 'none'
            })
          }
          // 获取所有页面栈实例
          let pages = getCurrentPages()
          // 当前页面实例
          let nowPage = pages[pages.length - 1]
          // 上一页面实例
          let prevPage = pages[pages.length - 2]
          prevPage.$vm.mesFormTop.row = {
            id: checked.row.id,
            purchase_code: checked.row.purchase_code
          }
          this.$tools.back()

          console.log('vm is', prevPage.$vm)
        }
      },
      cardForm: {
        valueName: 'purchase_code',
        // 未选择时候的提示语
        saveToast: '请选择生产计划',
        // 选中的字段数据
        checked: '',
        // 编辑
        edit: (index, item) => {
          console.log('index is', index, 'item is', item)
        },
        // 删除
        del: (index, item) => {
          console.log('index is', index, 'item is', item.row)
          // 这一步删除成功时必须要
          if (
            this.cardForm.checked == item.row[this.cardForm.valueName || 'id']
          ) {
            this.cardForm.checked = ''
          }
          // 自定义删除
          this.cardForm.items.splice(index, 1)
          console.log('删除之后的checked', this.cardForm.checked)
        },
        // 复选框变化
        checkChange: (e, row) => {
          console.log('选择改变', e, 'form', row)
        },
        // 需要循环的数据
        items: []
      }
    }
  },
  onLoad(options) {
    const purchase_code = options.purchase_code
    if (purchase_code) {
      this.cardForm.checked = purchase_code
    }
  },
  onShow() {
    this.resetPaging()
    this.getData()
  },
  onReachBottom() {
    if (!this.hasMore) return
    this.postForm.page++
    this.getData()
  },
  methods: {
    // 重置分页
    resetPaging() {
      this.hasMore = true
      this.postForm.page = 1
      this.cardForm.items = []
    },
    // 获取生产计划
    async getData() {
      let res = await this.$api.prod.plan.index(this.postForm)
      if (res.code == 200) {
        this.hasMore = res.data.list.length ? true : false
        const list = []
        res.data.list.forEach((item) => {
          list.push({
            row: item,
            // 编辑以及右侧内容，price可以不传
            header: {
              title: item.purchase_code,
              price: item.all_money
            },
            // 数据
            body: [
              {
                name: '单据日期：',
                value: item.purchase_time
              },
              {
                name: '交货时间：',
                value: item.delivery_time
              },
              {
                name: '车间：',
                value: item.workshop_id
              },
              {
                name: '数量：',
                value: item.number
              }
            ],
            //是否显示底部按钮
            footer: false
          })
        })
        this.cardForm.items = [...this.cardForm.items, ...list]
        this.noData = !this.cardForm.items.length
        console.log('生产计划', res)
      }
    }
  }
}
</script>
